# -----------------------------------------------------------------------------
# Copyright (c) 2025, Southeast University (China)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#  - Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  - Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  - Neither the name of the copyright holders nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Jiajie Xu, Yanfang Zhang, Jiaqi Gao, Leyun Tian
# -----------------------------------------------------------------------------


############# Open workspace ############
#open_workspace topDesign

############# Configuring parameter ##########
#set_parameter max_thread_number 8

# legalize related
set_parameter placement_legalization_mode {true} ;# set the eco placement to legalization mode
#set_placement_constraint -max_displacement {15 0}  ;# set the max displacement constraints for eco inst and original inst in the design. The unit is um.
#set_placement_constraint -max_displacement {10 0} -max_density 0.5 -design {sub} ;# set the max placement density constraint for design sub.
#set_placement_constraint -max_displacement {15 0} -max_congestion 0.9  ;# set the max routing congestion constraint.
#report_placement_constraint ;#report the placement constraint settings.
#report_placement_context  ;#report the placement context.

# eco buffer
#set_parameter eco_new_object_prefix {xtop}  ;# set the prefix for names of newly created cells or nets in eco actions.	
set_parameter eco_buffer_list_for_hold {BUFFD0BWP16P90  BUFFD0P75BWP16P90 BUFFD1BWP16P90 BUFFD2BWP16P90 BUFFD4BWP16P90 BUFFD8BWP16P90}  ;# set buffers used to fix hold violations in eco.
set_parameter eco_buffer_list_for_setup {BUFFD0BWP16P90  BUFFD0P75BWP16P90 BUFFD1BWP16P90 BUFFD2BWP16P90 BUFFD4BWP16P90 BUFFD8BWP16P90}  ;# set buffers used to fix setup, transition, capactance violations in eco.The 1st buffer specified in this parameter can also be used for si/fanout/wire length fixing.
#set_parameter eco_max_buffer_chain_length 8  ;# set the maximum buffer chain length inserted at one pin in automatic eco flow.

# size related
#set_parameter eco_buffer_group {BUF* DEL*} ;# set macro buffer group for buffers with different types.
set_parameter eco_cell_classify_rule {cell_attribute}  ;# set the cell classify rule for gate sizing: cell_attribute, nominal_keywords, and nominal_regex
set_parameter eco_cell_match_attribute {footprint} ;# set the size cell attribute matching type: footprint, user_function_class, pin_function
set_parameter eco_cell_nominal_swap_keywords {"ULVT" "LVT" ""}  ;# set the nominal keywords for swapping cells.
set_parameter eco_cell_nominal_sizing_pattern {D([0-16]+)BWP16}  ;# set the nominal pattern for sizing cells.

# remove buffer related
#set_parameter eco_remove_inverter_pair true ; enable remove inverter pair in remove buffer method.

# eco slack target and margin
set_parameter eco_setup_slack_target 0  ;# setup slack target for fixing setup violations, with unit ns.
#set_parameter eco_setup_slack_margin 0  ;# setup slack margin while fixing hold violations, with unit ns.
#set_parameter eco_hold_slack_target 0  ;# hold slack target for fixing hold violations, with unit ns.
set_parameter eco_hold_slack_margin -10  ;# hold slack margin while fixing setup violations, with unit ns.
#set_parameter eco_transition_slack_target 0  ;# transition slack target while fixing transition violations, with unit ns.
set_parameter eco_transition_slack_margin -10  ;# transition slack margin while fixing hold, setup, transition, capacitance violations, with unit ns.
#set_parameter eco_capacitance_slack_target 0  ;# capacitance slack target while fixing capacitance violations, with unit pF.
set_parameter eco_capacitance_slack_margin -10  ;# capacitance slack margin while fixing hold, setup, transition, capacitance violations, with unit pF.
set_parameter eco_gain_threshold 0.001  ;# acceptable minumum gain when fix hold/setup/transition, the unit is ns.

# user defined constraints
#set_parameter max_si 0.05  ;# define the allowed maximum delta delay caused by SI, with unit ns.
#set_parameter max_fanout 64  ;# define the allowed maximum fanout for pins.
#set_parameter max_wire_length 3000  ;# define the allowed max wire length for nets, with unit micrometer.

# skip timing commands
#set_skip_scenarios -min {sce1 sce2} true  ;# skip min timing for specified scenarios.
#set_skip_scenarios -max {sce3 sce4} true  ;# skip max timing for specified scenarios.

# set dont_touch
#set_dont_touch [get_pins pin_list] true  ;#skip violations on these pins.
#set_dont_touch [get_paths -start_points pin1 -end_points pin2 -delay_type min] true ;#Skip violation on the pins of these paths.
#set_dont_touch [get_cells cell_list] true ;#dont touch this cell including sizing. 
#set_dont_touch [get_nets net_list] true  ;#dont touch this net including insert buffer.
#set_dont_touch [get_io_path_pins] 1  ;#dont touch these pins on the paths from or to io of design
#set_dont_touch [get_lib_cells DEL*] 1  ;#dont touch the cell instances with these library cells
#set_hier_path_dont_touch {hier_path1 hier_path2} true  ;#dont touch hier path
#set_module_dont_touch {module1 module2} true  ;# dont touch module

# set only touch module
#set_module_dont_touch top_module true ;# dont touch all modules
#set_module_dont_touch {sub_module1 sub_module2} false ;# only touch these modules

# set only touch hier path
#set_hier_path_dont_touch / true ;# dont touch all hier paths
#set_hier_path_dont_touch {hier_path1 hier_path2} false ;# only touch these hier paths

# set dont use for size cell
#set_dont_use {TBUF*} true  ;# set library cells matched with specified patterns as dont use or not when sizing cell.

# set lib per instance
#set_lib_per_instance {regs/dram1/C3} {tutorialbc.idb} -corner fast  ;# set the libraries to use for the specified module instance.

# set lib cell for specified design
#set_specific_lib_cells -design sub [get_lib_cells *EP] -recursive  ;# set specific library cells for specified design.

########### Pre opt report #################
set report_dir "report"
exec rm -rf $report_dir
exec mkdir $report_dir
redirect -file $report_dir/mem.log {puts "Before opt the memory is [mem] KB."}
redirect -file $report_dir/pre_opt.rpt {summarize_gba_violations -exclude_path -as_reference -setup}
#redirect -file $report_dir/pre_opt.rpt -append {summarize_path_violations -as_reference}

########### Fix setup violations ###########
# Fix setup violations by GBA mode. (path slack is used)
#fix_setup_gba_violations -remove_buffer_only
#fix_setup_gba_violations -methods "size_cell" -effort high -setup_target 0.005 -hold_margin 0.0
#fix_setup_gba_violations -methods "size_cell" -size_rule nominal_keywords -setup_target 0.005 -hold_margin 0.0
#fix_setup_gba_violations -methods "insert_buffer" -setup_target 0.005 -hold_margin 0.0
fix_setup_gba_violations -effort high

# Fix setup violations by path. (only fix violations on path)
#fix_setup_path_violations -remove_buffer_only
#fix_setup_path_violations -methods "size_cell" -effort high -setup_target 0.005 -hold_margin 0.0
#fix_setup_path_violations -methods "size_cell" -size_rule nominal_keywords -setup_target 0.005 -hold_margin 0.0
#fix_setup_path_violations -methods "insert_buffer" -setup_target 0.005 -hold_margin 0.0

########### Post opt report ################
redirect -file $report_dir/mem.log -append {puts "After opt the memory is [mem] KB."}
redirect -file $report_dir/post_opt.rpt {summarize_eco_actions}
redirect -file $report_dir/post_opt.rpt -append {summarize_gba_violations -exclude_path -with_reference -with_delta -setup}
#redirect -file $report_dir/post_opt.rpt -append {summarize_path_violations -with_reference -with_delta}

########### Write ECO script ###############
set eco_output_dir ../eco_output/${top_design}
if {[file exists $eco_output_dir]} {
    exec rm -rf $eco_output_dir
}
exec mkdir $eco_output_dir
#write_design_changes -format ICC -eco_file_prefix xtop_opt_icc -output_dir $eco_output_dir
#write_design_changes -format ICC2 -eco_file_prefix xtop_opt_icc2 -output_dir $eco_output_dir
write_design_changes -format INNOVUS -eco_file_prefix xtop_opt_innovus -output_dir $eco_output_dir
#write_design_changes -format PT -eco_file_prefix xtop_opt_pt -output_dir $eco_output_dir

########### Save workspave #################
#save_workspace -as topDesign_opt
